﻿using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using Microsoft.Win32;

namespace FillMyMug
{
    public class ConfigurationInformation
    {
        private string _userName = "";
        private string _userPassword = "";
        private string _userDefaultAlbum = "Default Fill My Mug Album";
        private bool _saveUserLoginInfo = false;

        private const string subkeyLocation = "Software\\Loytty\\FillMyMug";

        static byte[] saltString = { 1, 9, 8, 7, 6, 5 };

        public ConfigurationInformation() {

            retrieveData();

        }
        ~ConfigurationInformation()
        {
            storeData();
        }

        public bool saveUserLoginInfo
        {
            set { _saveUserLoginInfo = value; }
            get { return _saveUserLoginInfo; }
        }

        public string userName
        {
            get { return _userName; }
            set { _userName = value; }
        }
        public string userPassword
        {
            get { return _userPassword; }
            set { _userPassword = value; }
        }
        public string defaultAlbum 
        { 
            set { _userDefaultAlbum = value; } 
            get { return _userDefaultAlbum; } 
        }

        private void retrieveData()
        {

            using (RegistryKey r = Registry.CurrentUser.OpenSubKey(subkeyLocation))
            {
                if (r == null)
                {
                    _userName = "";
                    _userPassword = "";
                    _userDefaultAlbum = "Default Fill My Mug Album";
                    _saveUserLoginInfo = false;

                    using (RegistryKey newRegKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(subkeyLocation))
                    {

                        newRegKey.SetValue("userName", ProtectedData.Protect((new UnicodeEncoding().GetBytes("DUMMY VALUE")), saltString, DataProtectionScope.CurrentUser));
                        newRegKey.SetValue("userPassword", ProtectedData.Protect((new UnicodeEncoding().GetBytes("DUMMY VALUE TWO")), saltString, DataProtectionScope.CurrentUser));

                        newRegKey.SetValue("userDefaultAlbum", _userDefaultAlbum);
                        newRegKey.SetValue("saveUserInfo", false);
                        newRegKey.Close();
                    }
                    
                    
                }
                else
                {

                    byte[] userData = ProtectedData.Unprotect((byte[])r.GetValue("userName"), saltString, DataProtectionScope.CurrentUser);
                    byte[] passwordData = ProtectedData.Unprotect((byte[])r.GetValue("userPassword"), saltString, DataProtectionScope.CurrentUser);

                    _userName = (new UnicodeEncoding().GetString(userData));
                    _userPassword = (new UnicodeEncoding().GetString(passwordData));
                    
                    _userDefaultAlbum = (null == r.GetValue("userDefaultAlbum") ? "Default Fill My Mug Album" :(string) r.GetValue("userDefaultAlbum"));
                    _saveUserLoginInfo = (null == r.GetValue("saveUserInfo")?false:Convert.ToBoolean(r.GetValue("saveUserInfo")));
                    r.Close();
                }
                
                
            }

        }
        private void storeData()
        {

            try
            {
               
                
                using (RegistryKey r = Registry.CurrentUser.OpenSubKey(subkeyLocation,true))
                {
                    if (r == null)
                    {
                        throw new NullReferenceException();
                    }
                    else
                    {
                        if (_saveUserLoginInfo)
                        {
                            r.SetValue("userName", ProtectedData.Protect((new UnicodeEncoding().GetBytes(_userName)), saltString, DataProtectionScope.CurrentUser));
                            r.SetValue("userPassword", ProtectedData.Protect((new UnicodeEncoding().GetBytes(_userPassword)), saltString, DataProtectionScope.CurrentUser));
                        }
                        r.SetValue("userDefaultAlbum", _userDefaultAlbum);
                        r.SetValue("saveUserInfo", _saveUserLoginInfo);
                        
                        r.Close();
                    }

                }
            }
            catch
            {
                throw;
            }
        }


    }
}
